import 'package:cleanetc_client/core/utils/toast.dart';
import 'package:cleanetc_client/generated/l10n.dart';
import 'package:cleanetc_client/mvvm/viewModel/base_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/icon_data.dart';
import 'package:oktoast/oktoast.dart';

class LoginViewModel extends BaseApiViewModel {
  FocusNode blankNode = FocusNode();
  final visibility = ValueNotifier(Icons.visibility);
  ValueNotifier<bool> isShowPassWord = ValueNotifier(false);

  TextEditingController unameController = TextEditingController();
  TextEditingController pwdController = TextEditingController();

  LoginViewModel(BuildContext context) : super(context) {
    isShowPassWord.addListener(() {
      if (isShowPassWord.value) {
        visibility.value = Icons.visibility;
      } else {
        visibility.value = Icons.visibility_off;
      }
    });
  }

  showPassWord() {
    isShowPassWord.value = !isShowPassWord.value;
  }

  void closeKeyboard(BuildContext context) {
    FocusScope.of(context).requestFocus(blankNode);
  }

  void doLogin() {
    doPost("/user/login", {
      "username": unameController.text,
      "password": pwdController.text
    }).onData((data) {
      showToast(data.toString());
    });
  }
}
